package com.yeyks.consumer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeyks.consumer.dal.dao.UserWxidRelationMapper;
import com.yeyks.consumer.dal.domain.UserWxidRelation;
import com.yeyks.consumer.param.CashOutOrder.CashOutOrderBindWxParam;
import com.yeyks.consumer.service.UserWxidRelationService;
import com.yeyks.consumer.vo.CashOutOrder.CashOutOrderIsBindWxVo;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * <p>
 * 用户提现微信关系表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-12-03
 */
@Service
public class UserWxidRelationServiceImpl extends ServiceImpl<UserWxidRelationMapper, UserWxidRelation> implements UserWxidRelationService {

    //根据用户id查询
    @Override
    public UserWxidRelation getByUserId(Integer userId) {
        UserWxidRelation userWxidRelation = getOne(
                new QueryWrapper<UserWxidRelation>()
                        .eq(UserWxidRelation.USER_ID, userId)
                        .last("limit 1")
        );
        return userWxidRelation;
    }

    //是否绑定微信
    @Override
    public CashOutOrderIsBindWxVo isBindWx(Integer userId) {
        UserWxidRelation userWxidRelation = getByUserId(userId);
        CashOutOrderIsBindWxVo vo = new CashOutOrderIsBindWxVo();
        if (Objects.nonNull(userWxidRelation)) {
            BeanUtil.copyProperties(userWxidRelation, vo);
            vo.setFlag(true);
        } else {
            vo.setFlag(false);
        }
        return vo;
    }

    //绑定微信
    @Override
    public Boolean bindWx(CashOutOrderBindWxParam param) {
        try {
            UserWxidRelation userWxidRelation = BeanUtil.toBean(param, UserWxidRelation.class);
            return save(userWxidRelation);
        } catch (Exception e) {
            return false;
        }
    }

}